﻿using System;
using System.Configuration;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using mojoPortal.Web.RS.SBM.AppCode.Lib;


namespace mojoPortal.Web.RS.SBM
{
    public partial class RSCheckoutEquip : RSBasePage
    {
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
        }

        
        protected void Page_Load(object sender, EventArgs e)
        {
            
            if (!Page.IsPostBack)
            {
                InitializeReadyServicePage("Inventory");
                RSUserIdentity userIdentity = GetRSUserIdentity();
                int companyid = userIdentity.RSCompanyId;
                int WorkOrderid = Convert.ToInt32(Request.QueryString["workorderid"].ToString());
                LoadInventoryList(companyid, WorkOrderid);
                ucCheckoutEquipListControl.Visible = false;
            }
        }
        protected void gvInventoryItem_Edit(object sender, CommandEventArgs e)
        {
            RSUserIdentity userIdentity = GetRSUserIdentity();
            int companyid = userIdentity.RSCompanyId;
            int productid = Convert.ToInt32(e.CommandArgument.ToString());
            if (companyid > 0 && productid > 0)
                ucModalProductControl.ShowModal(companyid, productid, true);
        }

        protected void btnReturn_Click(Object sender, EventArgs e)
        {
            int WorkOrderid = 0;
            if (Request.QueryString["workorderid"] != null)
                WorkOrderid = Convert.ToInt32(Request.QueryString["workorderid"].ToString());
            Response.Redirect("~/RS/SBM/RSWorkOrderEquips.aspx?&workorderid=" + WorkOrderid);
        }
        private void ViewCheckoutEquip()
        {
            RSUserIdentity userIdentity = GetRSUserIdentity();
            int companyid = userIdentity.RSCompanyId;
            ucCheckoutEquipListControl.ShowEquipmentList(companyid);
            ucCheckoutEquipListControl.Visible = true;
        }
        protected void btnCheckoutEquip_OnClick(Object sender, EventArgs e)
        {
            TextBox txtQty = gvInventoryList.SelectedRow.FindControl("txtQuantity") as TextBox;
            
            if (txtQty != null && txtQty.Text.Length > 0 && RSSBMCommonFunctions.IsNumeric(txtQty.Text) == true)
            {
                int qty = Convert.ToInt32(txtQty.Text);
                int productid = Convert.ToInt32(gvInventoryList.SelectedValue.ToString());
                List<CheckoutEquip> selParts = new List<CheckoutEquip>();
                if (Session["CHECKOUT_EQUIP"] != null)
                    selParts = (List<CheckoutEquip>)Session["CHECKOUT_EQUIP"];
                //Remove any duplicate
                if (selParts.Count > 0)
                {
                    for (int i = 0; i < selParts.Count; i++)
                    {
                        if (selParts[i].ProductId == productid)
                        {
                            selParts.RemoveAt(i);
                            break;
                        }
                    }
                }
                txtQty.Text = "";
                selParts.Add(new CheckoutEquip(productid, qty, "", 0, 0, ""));
                Session["CHECKOUT_EQUIP"] = selParts;
                SetSelectedRowState(false);
                gvInventoryList.SelectedIndex = -1;

            }
            ViewCheckoutEquip();
        }
        private void SetSelectedRowState(bool bstate)
        {
            if (gvInventoryList.SelectedIndex >= 0)
            {
                TextBox txtQty = gvInventoryList.SelectedRow.FindControl("txtQuantity") as TextBox;
                if (txtQty != null)
                {
                    txtQty.Enabled = bstate;
                    if (bstate == false)
                        txtQty.BackColor = System.Drawing.Color.Gainsboro;
                    else
                        txtQty.BackColor = System.Drawing.Color.White;

                }
                Button btnAdd = gvInventoryList.SelectedRow.FindControl("btnCheckoutEquip") as Button;
                if (btnAdd != null)
                    btnAdd.Visible = bstate;
            }
        }
        protected void gvInventoryList_SelectedIndexChanging(Object sender, EventArgs e)
        {
            SetSelectedRowState(false);
        }
        protected void gvInventoryList_SelectedIndexChanged(Object sender, EventArgs e)
        {
            SetSelectedRowState(true);
        }
        public void LoadInventoryList(int companyid, int WorkOrderid)
        {
            SqlCommand myCommand;
            SqlParameter param;
            SqlConnection myConnection;

            String sqlSELECT = "SELECT c.ProductId,c.ProductName,c.QuantityPerUnit,c.UnitPrice,c.UnitsInStock,c.UnitsOnOrder, c.ReorderLevel,c.ProductDesc," +
                                "c.Manufacture,c.Discontinued,c.ModelNo,c.ProductCode,c.ParentId, (SELECT p.ProductName FROM rs_tblInventory p WHERE p.ProductId=c.ParentId) as CategoryName,c.SupplierID,c.CompanyId " +
                                " FROM rs_tblInventory c WHERE c.IsEquipment=1 AND c.companyid=@companyid AND c.ParentId > 0 AND c.ProductId NOT IN(SELECT o.ProductId FROM rs_tblWorkOrderPart o WHERE o.workorderid=@WorkOrderId)  order by CategoryName ";

            myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings[GetRSCompanyConnectionString()].ToString());
            myCommand = new SqlCommand(sqlSELECT, myConnection);

            param = new SqlParameter();
            param.SqlDbType = SqlDbType.Int;
            param.ParameterName = "@companyid";
            param.Value = companyid;
            myCommand.Parameters.Add(param);

            param = new SqlParameter();
            param.SqlDbType = SqlDbType.Int;
            param.ParameterName = "@WorkOrderid";
            param.Value = WorkOrderid;
            myCommand.Parameters.Add(param);

            SqlDataAdapter adapter = new SqlDataAdapter(myCommand);
            myConnection.Open();
            DataSet ds = new DataSet();
            adapter.Fill(ds);

            gvInventoryList.DataSource = ds;
            gvInventoryList.DataBind();
            myConnection.Close();

        }

    }
}


